import SDKAd from "./SDKAd";
export default class SDKGamePortalAd extends SDKAd {

    open() {
        this.create()
        this.load()
    }


    create() {

        if (!this.instance) {
            this.instance = this.sdk.createGamePortalAd({
                adUnitId: this._adUnitId
            })
            this.instance.onLoad(this.bind(this.onLoad))
            this.instance.onClose(this.bind(this.onClose))
            this.instance.onError(this.bind(this.onError))
        }

    }

    load() {
        this.instance.load().then(function () {
            console.log('load success')

        }).catch(function (error: any) {
            console.log('load fail with:' + error.errCode + ',' + error.errMsg)
        })
    }

    show() {
        this.instance.show()
    }

    onError(err: any) {
        console.log(err)
    }


    onLoad() {
        console.log('load success')
        this.show();
    }

    onClose() {
        console.log('互推盒子九宫格广告关闭')
    }

    destroy(): void {
        if (this.instance) {
            this.instance.offLoad(this.unbind(this.onLoad))
            this.instance.offClose(this.unbind(this.onClose))
            this.instance.offError(this.unbind(this.onError))
            this.instance.destroy()
            this.instance = null;
        }
    }
}